# SPDX-FileCopyrightText: 2022 Darren <1912544842@qq.com>
# SPDX-License-Identifier: Apache-2.0

# ylc 修改 

set(CMAKE_SYSTEM_NAME Generic)
set(CMAKE_SYSTEM_VERSION 1)
set(CMAKE_TRY_COMPILE_TARGET_TYPE "STATIC_LIBRARY")
cmake_minimum_required(VERSION 3.19)    #gcc版本说明

# 工具链设置
set(TOOLPATH  "D:/MounRiver/MounRiver_Studio/toolchain/RISC-V Embedded GCC/bin/riscv-none-embed-")  #ylc
set(CMAKE_C_COMPILER ${TOOLPATH}gcc.exe)
set(CMAKE_CXX_COMPILER ${TOOLPATH}g++.exe)
set(CMAKE_ASM_COMPILER ${TOOLPATH}gcc.exe)
set(CMAKE_AR ${TOOLPATH}ar.exe)
set(CMAKE_OBJCOPY ${TOOLPATH}objcopy.exe)
set(CMAKE_OBJDUMP ${TOOLPATH}objdump.exe)
set(SIZE ${TOOLPATH}size.exe)
set(CMAKE_TRY_COMPILE_TARGET_TYPE STATIC_LIBRARY)
  

# 项目设置
project(mc3172-cmake C)     #工程名称
set(CMAKE_CXX_STANDARD 17)
set(CMAKE_C_STANDARD 99)

# 编译参数 一般不用改
add_compile_options(-march=rv32imc -mabi=ilp32 -std=gnu99 )#-Wall
add_compile_options(-mtune=size -msmall-data-limit=8 -mstrict-align -mno-save-restore -mno-div -mbranch-cost=1)
add_compile_options(-fmessage-length=0 -fsigned-char -ffunction-sections -fdata-sections -fno-common -ffreestanding -fno-builtin)
# 编译等级
add_compile_options(-O0)

# 宏定义
#add_definitions()


# 头文件路径                                         #ylc
include_directories(
    Setting
    Drivers
    User
    Third_Party/Printf
    Third_Party/IIC
    Third_Party/HMC5883
    Third_Party/DHT11
    "D:/MounRiver/MounRiver_Studio/toolchain/RISC-V Embedded GCC/riscv-none-embed/include"
    "D:/MounRiver/MounRiver_Studio/toolchain/RISC-V Embedded GCC/lib/gcc/riscv-none-embed/8.2.0/include"
    "D:/MounRiver/MounRiver_Studio/toolchain/RISC-V Embedded GCC/lib/gcc/riscv-none-embed/8.2.0/include-fixed"
)

 
# 源码文件                                          #ylc
file(GLOB_RECURSE SOURCES 
                "Drivers/*.c"
                "User/*.c"
                "Third_Party/Printf/*.c"
                "Setting/*.c"
                "Third_Party/IIC/*.c"
                "Third_Party/HMC5883/*.c"
                "Third_Party/DHT11/*.c"
                )

# 链接参数 -nodefaultlibs -nostdlib                 #ylc
set(LINKER_SCRIPT  ${CMAKE_SOURCE_DIR}/Setting/MC3172.lds)
add_link_options(-nostartfiles 
                
                -Xlinker --gc-sections  
                -Wl,--print-memory-usage
                -Wl,-Map,${PROJECT_NAME}.map 
                --specs=nano.specs 
                --specs=nosys.specs)
add_link_options(-T ${LINKER_SCRIPT})


# 编译可执行文件
add_executable(${PROJECT_NAME}.elf ${SOURCES} ${LINKER_SCRIPT})

 
# 输出hex和bin
set(HEX_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.hex)
set(BIN_FILE ${PROJECT_BINARY_DIR}/${PROJECT_NAME}.bin)
add_custom_command(TARGET ${PROJECT_NAME}.elf POST_BUILD
        COMMAND ${CMAKE_OBJCOPY} -Oihex $<TARGET_FILE:${PROJECT_NAME}.elf> ${HEX_FILE}
       # COMMAND ${CMAKE_OBJCOPY} -Obinary $<TARGET_FILE:${PROJECT_NAME}.elf> ${BIN_FILE}
) 